﻿using Edu.Http;
using Edu.Http.Extensions;
using Edu.Http.Proxy;
using Infrastructure.Config.Core.External;
using Infrastructure.ExternalService.Core.Responses;
using Infrastructure.ExternalService.Core.StudentService;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System.Collections.Generic;
using System.Net.Http;
using System.Threading.Tasks;

namespace Infrastructure.ExternalService.Core.Imp.Product
{
    /// <summary>
    /// 商品查询服务
    /// </summary>
    public class EduStudentService : BaseHttpClient, IEduStudentService
    {
        private readonly OrderCenterApiOptions _orderingOptions;
        public IAccessTokenProvider TokenProvider { get; set; }

        public EduStudentService(IOptions<OrderCenterApiOptions> options, HttpClient client, ILoggerFactory loggerFactory)
            : base(client, loggerFactory, options.Value.StudentOptions.EndPoint)
        {
            _orderingOptions = options.Value ?? throw new HttpProxyException($"{nameof(EduStudentService)}配置获取失败");
            loggerFactory.CreateLogger<EduStudentService>();
        }

        /// <summary>
        /// 设置BaseHttpClient的BaseAddress：该值默认是从构造函数传入的，但重写该方法可以改变该值
        /// </summary>
        /// <returns></returns>
        protected override string SetHttpClientBaseAddress()
        {
            return null;
        }

        public async Task<List<EduOrderInfoResponse>> GetOrderInfoAsync(EduOrderInfoRequest request)
        {
            var formStr = ConvertRequestToCommonFormUrlEncode(request);

            var response =
                await PostAsync<WrapResponse<List<EduOrderInfoResponse>>>(_orderingOptions.StudentOptions.GetOrderInfo, request);
            if (response.Code.Equals(ClientReturnCode.Success) && response.Data != null)
            {
                return response.Data;
            }

            //if (response.Data == null)
            //{
            //    throw new HttpProxyException($"查询【{formStr}】商品信息的外部接口返回错误:【{response.Msg}】");
            //}


            throw new HttpProxyException($"{response.Msg}");
        }


    }
}